package 面向对象.day05;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author 王敬仟
 * World 整个游戏
 */
public class World extends JPanel{
    //设置游戏窗口的宽 静态的不可改变的
    public static final int WIDTH=641;
    //设置游戏窗口的高 静态的不可改变的
    public static final int HEIGHT=479;
    //准备战舰
    private Battleship ship=new Battleship();
    //准备海洋对象数组
    private SeaObject[] submarines={};
    //准备鱼雷
    private Mine[] mines={};
    //准备炸弹
    private Bomb[] bombs={};
    //随机生成潜艇对象
    public SeaObject nextSeaObject(){
        Random rand=new Random();
        //定义海洋对象的总和
        int type=rand.nextInt(20);
        if(type<8){
            //生成鱼雷潜艇
            return new MineSubmarine();
        }else if (type<12){
            //生成侦查潜艇
            return new ObserverSubmarine();
        }else {
            //生成水雷潜艇
            return new TorpedoSubmarine();
        }
    }
    //设置初始时间为0
    private int subEnterIndex=0;
    //潜艇入场
    public void submarineEnterAction(){
        subEnterIndex++;
        //设置每400毫秒走一次
        if(subEnterIndex%40==0){
            //下一个对象
            SeaObject obj=nextSeaObject();
            //把新的对象加到数组中去
            submarines= Arrays.copyOf(submarines,submarines.length+1);
            //把数组缩容
            submarines[submarines.length-1]=obj;
        }
    }
    //设置初始时间为0
    private int mineEnterIndex=0;
    //鱼雷 水雷入场
    public void mineEnterAction(){
        mineEnterIndex++;
        if (mineEnterIndex%100==0){

        }
    }
    /*海洋对象移动*/
    public void moveAction(){
        //所有潜艇的移动
        for (int i=0;i<submarines.length;i++){
            submarines[i].move();
        }
        //炸弹对象移动
        for (int i = 0; i < this.bombs.length; ++i){
            bombs[i].move();
        }
        //鱼雷对象移动
        for (int i = 0; i < this.mines.length; ++i){
            mines[i].move();
        }
    }
    /*判断海洋对象越界*/
    public void outOfBoundsAction(){
        //便利海洋潜艇对象
        for (int i = 0; i < submarines.length; ++i){
            //判断潜艇对象是否越界
            if (submarines[i].isOutOfBounds()){

            }
        }
        //便利所有的鱼雷
        for (int i = 0; i < mines.length; ++i){
            //判断鱼雷是否越界
            if (mines[i].isOutOfBounds()){

            }
        }
        //便利所有的炸弹
        for (int i = 0; i < bombs.length; ++i){
            //判断炸弹是否越界
            if (bombs[i].isOutOfBounds()){

            }
        }
    }
    /** 启动程序的运行 */
    public void action(){
        KeyAdapter k= new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                //VK_SPACE空格键 判断松开空格键发炸弹
                if(e.getKeyCode() == KeyEvent.VK_SPACE){
                    Bomb obj = ship.shoot(); //深水炸弹入场
                    bombs = Arrays.copyOf(bombs,bombs.length+1);
                    bombs[bombs.length-1] = obj;
                }
                //判断VK_LEFT左键移动
                if(e.getKeyCode() == KeyEvent.VK_LEFT){
                    ship.moveLeft();
                }
                //判断右键移动
                if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                    ship.moveRight();
                }
            }
        };
        this.addKeyListener(k);
        Timer timer = new Timer();
        int interval = 10;
        timer.schedule(new TimerTask() {
            public void run() {
                submarineEnterAction(); //(侦查潜艇,鱼雷潜艇,水雷潜艇)入场
                mineEnterAction();//(鱼雷,水雷)入场
                moveAction();//海洋对象的移动
                outOfBoundsAction();//删除越界的海洋对象
                repaint();
            }
        }, interval, interval);
    }
    //往游戏窗口中画对象
    public void paint(Graphics g){
        //画海洋图
        Iamges.sea.paintIcon(null,g,0,0);
        //跟中海洋图 并画出海洋图
        ship.paintImage(g);
        //便利海洋潜艇对象
        for (int i=0;i<submarines.length; ++i){
            //画潜艇对象
            submarines[i].paintImage(g);
        }
        //便利所有的炸弹
        for (int i = 0; i < this.bombs.length; ++i){
            //画炸弹
            bombs[i].paintImage(g);
        }
        //便利所有的鱼雷
        for (int i = 0; i < mines.length; ++i){
            //画鱼雷
            mines[i].paintImage(g);
        }
    }
    //画窗口
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        World world = new World();
        world.setFocusable(true);
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH+16, HEIGHT+39);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }
}
